
$(function(){
	
	//注册协议
	$("#agreed").on("click",function(){
		if($('#agreed').is(':checked')){
			$("#register").removeAttr("disabled").css({"background":"#189AE5","border-color":"#189AE5"});
		}else{
			$("#register").attr("disabled","disabled").css({"background":"#71BFF2","border-color":"#71BFF2"});
			 $("#span5").html("请仔细阅读完协议后,选择同意再进行注册!");
		}
	});
	
	$("#register").on("click",function(){
		registerUser();
	});
	
});

/*验证码*/
function changeImg(){
	var code=document.getElementById("code");
  	code.src="servlet/ServletCode?"+new Date();
}

function register(username,password,email){
	var  url = "public/user/registerUser";
	password = $.md5(password + username);
	var params = {username:username,password:password,email:email};
	 ajaxAsyncResult(url,params,function(success,result){
		 if(success){
			//注册成功，跳转
			 location.href = "public/user/registerSuccess";
		 }else{
			 if(result.code == -1){//帐号已被注册
					$("#span1").html(result.message).css("color","#00BC3E");
				}else if(result.code == -2){//邮箱已存在
					$("#span2").html(result.message).css("color","#00BC3E");
				}else if(result.code == -3){
					$.util.alert(result.message);
				}
		 }
	 });
}

/**
 * 注册直接提交验证
 */
function registerUser(){
	var username = $("#username").val();
	var password = $("#password").val();
	var confirmPassword = $("#confirmPassword").val();
	var email = $("#email").val();
	if(!username || !(/^\w{5,18}$/.test(username))){
	  	$("#span1").html("用户名不能为空必须是5~18位!").css("color","#00BC3E");
	  	return false;
	  }else{
		  $("#span1").html("");
	  }
	 if(!password ||!(/^\w{6,15}$/.test(password))){
		 $("#span3").html("密码不能为空必须是6~15位!").css("color","#00BC3E");
		 return false;
	  }else{
	  		$("#span3").html("");
	  }
	
	 if(password != confirmPassword ){
		 $("#span4").html("两次输入的密码不一致!").css("color","#00BC3E");
		 return false;
	 }else{
  		 $("#span4").html("");
	  }
	 
	 if(!email  || !(/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(email))){
		 $("#span2").html("邮箱不能为空,且必须符合邮箱格式!").css("color","#00BC3E");
		 return false;
	 }else{
		  $("#span2").html("");
	  }
	 
	 register(username,password,email);
	 
}

/**
 * 登陆检查
 * sourceType=1首页登陆
 * sourceType=2 弹框登录
 */

function checkUser(sourceType){
	var username = $("#username").val();
	if (!username && username != 0) {
		$("#span1").html("用户名不能为空!").css("color", "#00BC3E");
		return false;
	} else {
		$("#span1").html("");
	}
	var password =$("#password").val();
	if (!password && password != 0) {
		$("#span2").html("密码不能为空!").css("color", "#00BC3E");
		return false;
	} else {
		$("#span2").html("");
	}
	loginCheck(sourceType,username,password);
}

/**
 * 登录
 */
function loginCheck(sourceType,username,password){
	$("#login-button").val("登陆中,请稍等...");
	 password = $.md5(password + username);
	 var url = "public/user/checkLogin";
	 var params = {"username" : username,"password" : password};
	ajaxAsyncResult(url,params,function(success,result){
		if (success) {
			if (sourceType == 1) {
				window.location.href = 'public/index';
			}else{//通过弹出框登陆的
				$('body .win-mydiv').css("display","none");
				$('body .win-bg').css("display","none");
				location.reload();
			}
		}else{
			if (result.code == -3) {
				$("#span1").html(result.message);
				$("#span2").html("");
			} else if (result.code == -2) {
				$("#span1").html("");
				$("#span2").html(result.message);
			} else {// 0被锁定，-1未认证,2未激活
				$("#span1").html(result.message);
			}
			$("#login-button").val("登陆");
		}
	});
}

/**
 * 注册邮箱激活
 */
function activate(){
	
	 var url = "public/user/registerActivateStatus";
	 var params = {"username" : $('#username').val()};
	ajaxAsyncResult(url,params,function(success,result){
		if (success) {
			$.util.alert("OK");
		}else{
			$.util.alert("ERROR");
		}
	});
}







